const { sequelize } = require('../utils/sequelize');
const { v4: uuidv4 } = require('uuid');

class AcademyService {
    constructor() {
    }

    async findAll(){
        let data = null;
        data = await sequelize.models.Academy.findAll();
        return data;
    }

    async findById(academyId){
        let data = null;
        data = await sequelize.models.Academy.findOne({
            where: {academy_id: academyId}});
        
        return data;
    }

    async saveAcademy(academyObj){
        let data = null;
        data = await sequelize.models.Academy.create({
            academyId: uuidv4(),
            academyName: academyObj.academyName
        })
        return data;
    }

    async deleteAcademy(academyId){
        let data = null;
        let hasUse = await sequelize.models.User.findOne({
            where: {
                academyId: academyId
            }
        })
        if(hasUse) return { name: '还存在该学院的学生，不能删除'}
        
        data = await sequelize.models.Academy.destroy({
            where: {academy_id: academyId}});
        return data;
    }

    async updateAcademy(academy){
        let data = null;
        let academyObj = {
            academyName: academy.academyName,
        }
        data = await sequelize.models.Academy.update({...academyObj}, {
            where: {academyId: academy.academyId}
        })
        return data;
    }
}

module.exports = new AcademyService();